import React from 'react';
import { useCollapse } from '@/hooks';
import { Collapse } from '@/molecules';
import { Form } from 'antd';
import {
    BottomAction,
    AssessKPI,
    DiscussArrange,
    AssessResultDynamic,
    AssessLeader,
    PassProbation
} from '../containers';
import { useAssessSetting, useAssessTotal, useAssessLevel } from '../hooks';
import { TRIAL_ACTION, TALK } from '../constants';

const { Panel } = Collapse;

function AssessLeaderQ({ form, data = {} }) {
    const { assessQuarterly = [], interviewJoinCodes = [], employeeInfo = {}, assessAbility } = data;
    const { levels } = assessAbility || {};
    const assessTotal = useAssessTotal(data, form);
    const level = useAssessLevel(levels, assessTotal?.level);

    const detail = React.useMemo(
        () => assessQuarterly.find(item => item.status === TRIAL_ACTION.ASSESS_TEACHER_QUARTER) || {},
        [assessQuarterly]
    );

    const { activeKey, onChange, openAll } = useCollapse(['1', '2', '3', '4'], []);
    const { q = {} } = useAssessSetting(data);

    const isBad = level === 1;
    const isGood = level === 3;
    const showPassProbation = employeeInfo?.workNature === 'full_time' && isGood;
    const { regularInterview } = form.getFieldsValue();
    const showDiscuss = isBad || (isGood && regularInterview === '1');
    const initInterviewType = isBad ? TALK.QUARTER : TALK.AHEAD_FORMAL;

    const params = React.useMemo(() => {
        const {
            resourceRelation = {},
            assessSummary = 0,
            project,
            interviewWay,
            interviewLocation,
            interviewEmployeeCodeList,
            interviewDate,
            interviewStartTime,
            interviewEndTime,
            interviewType,
            schedule,
            ...values
        } = form.getFieldsValue();
        const interviewData = {
            interviewType: interviewType || initInterviewType,
            interviewWay,
            interviewLocation,
            interviewEmployeeCodeList,
            interviewStartTime: `${interviewDate?.format('YYYY-MM-DD')} ${interviewStartTime?.format('HH:mm:00')}`,
            interviewEndTime: `${interviewDate?.format('YYYY-MM-DD')} ${interviewEndTime?.format('HH:mm:00')}`,
            schedule
        };
        const interviewArrange = showDiscuss ? { interviewArrange: interviewData } : {};
        const { uploadResourceFile } = resourceRelation;
        return {
            assess: {
                resourceRelation: uploadResourceFile ? [uploadResourceFile] : [],
                assessAbility: {
                    project
                },
                assessSummary: assessSummary[0],
                level: assessTotal.level, // 综合能力等级
                score: assessTotal.score, // 综合能力分数
                ...interviewArrange,
                ...values
            }
        };
    }, [form]);

    return (
        <div>
            <Collapse bordered={false} activeKey={activeKey} onChange={onChange}>
                <AssessResultDynamic data={data} />
                {q.showKPI && Boolean(data?.performanceTarget?.result) && (
                    <Panel header="季度绩效目标" key="2">
                        <AssessKPI data={data} />
                    </Panel>
                )}
                {q.showAssess && (
                    <Panel header="能力评估" key="3">
                        <AssessLeader form={form} data={data} detail={detail} assessTotal={assessTotal} />
                        {showPassProbation && <PassProbation form={form} />}
                    </Panel>
                )}
                {q.showAssess && showDiscuss && (
                    <Panel header="新员工面谈安排" key="4">
                        <DiscussArrange
                            data={{
                                interviewEmployeeCodeList: interviewJoinCodes,
                                interviewWay: 1
                            }}
                            interviewType={initInterviewType}
                            form={form}
                        />
                    </Panel>
                )}
            </Collapse>

            <BottomAction openAll={openAll} data={data} params={params} form={form} />
        </div>
    );
}

export default Form.create({})(AssessLeaderQ);
